package com.woniuxy.woniuhis.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.woniuhis.entity.DocAdvice;
import com.woniuxy.woniuhis.entity.Inpatient;
import com.woniuxy.woniuhis.entity.MediItem;
import com.woniuxy.woniuhis.entity.RecipeMedi;
import com.woniuxy.woniuhis.exception.InpatientServiceException;
import com.woniuxy.woniuhis.service.MediItemService;
import com.woniuxy.woniuhis.service.RecipeMediService;
import com.woniuxy.woniuhis.util.ResponseInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;

@Controller
@RequestMapping("/mediItem")
public class MediItemController {
    @Autowired
    private MediItemService mediItemService;
    @Autowired
    private RecipeMediService recipeMediService;

    @RequestMapping("delMediItemByIds")
    @ResponseBody
    public ResponseInfo delMediItemByIds(String[] mediItemId) {
        ResponseInfo responseInfo = new ResponseInfo();
        try {
            if (mediItemId != null) {
                for (int i = 0; i < mediItemId.length; i++) {
                    mediItemService.delMediItemById(mediItemId[i]);
                }
            }
            System.out.println(Arrays.toString(mediItemId));
            responseInfo.setState(200);
            responseInfo.setData("success");
            return responseInfo;
        } catch (InpatientServiceException e) {
            e.printStackTrace();
            responseInfo.setState(500);
            responseInfo.setMsg(e.getMessage());
            return responseInfo;
        }
    }

    @RequestMapping("findMediItemById")
    @ResponseBody
    public ResponseInfo findMediItemById(String mediItemId) {
        ResponseInfo responseInfo = new ResponseInfo();
        try {
            if (!StringUtils.isEmpty(mediItemId)) {
                MediItem mediItemById = mediItemService.findMediItemById(mediItemId);
                responseInfo.setState(200);
                responseInfo.setData(mediItemById);
            }
            return responseInfo;
        } catch (InpatientServiceException e) {
            e.printStackTrace();
            responseInfo.setState(500);
            responseInfo.setMsg(e.getMessage());
            return responseInfo;
        }
    }

    @RequestMapping("updateMediItemById")
    @ResponseBody
    public ResponseInfo updateMediItemById(String mediItemId, Integer amount) {
        ResponseInfo responseInfo = new ResponseInfo();
        try {
            System.out.println(mediItemId + "--------" + amount);
            if (!StringUtils.isEmpty(mediItemId) && amount != null && amount != 0) {
                //更新条目信息和处方信息
                MediItem mediItemById = mediItemService.findMediItemById(mediItemId);
                Double singlePrice = (mediItemById.getMediItemSubtotal() / mediItemById.getAmount());
                Double money = singlePrice * (amount - mediItemById.getAmount());
                mediItemById.setMediItemSubtotal(amount * singlePrice);
                mediItemById.setAmount(amount);
                System.out.println(mediItemId);
                mediItemService.modifyMediItem(mediItemById);
                //更新处方信息
                RecipeMedi recipeMediById = recipeMediService.findRecipeMediById(mediItemById.getRecipeMedi().getRecipeMediId());
                recipeMediById.setRecipeMediTotal(recipeMediById.getRecipeMediTotal() + money);
                recipeMediService.modifyRecipeMedi(recipeMediById);
                responseInfo.setState(200);
                responseInfo.setData("success");
            }
            return responseInfo;
        } catch (InpatientServiceException e) {
            e.printStackTrace();
            responseInfo.setState(500);
            responseInfo.setMsg(e.getMessage());
            return responseInfo;
        }
    }
}
